// ===== 引入工具类
const util = require( "./util/util");
const dbUtil = require( "./util/dbUtil");
const apiUtil = require("./util/apiUtil");

exports.test = async (context) => {
    return '杰克琼斯';
};

/**
 * 商家登录
 * @param {*} context 默认请求参数上下文
 * @param {*} context.data {object}
 * @apiParam user_nick 用户昵称
 */
exports.merchantLoginb = async (context) => {
    // 参数校验 ---------- start
    let param = {
        access_token:context.accessToken,
        open_id:context.openId,
        user_name:context.data.user_nick,
        mix_nick:context.mixNick
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:param.open_id});
    let res = await dbUtil.insertOrUpdate(context,'b_merchant',merchant,param);
    return res;
}


/**
 * 清除商品id中的空格
 */
exports.clearSpaceForC_currency_convert_goods_info = async (context) => {
    const tableName = "c_currency_convert_goods_info";
    const list = await dbUtil.selectListAll(context,tableName,{},{});
    for(let item of list){
        item.goods_id = item.goods_id.trim();
        await dbUtil.updateById(context,tableName,item,item);
    }
    return list;
}


/**
 * 获取统计数据
 */
exports.getData = async (context) => {
    const result = [];
    
    // -------------- 每天只能打一次卡 start
    let time_start = await util.getTimeByDay(0);// 获取当天开始时间
    let time_end = await util.getTimeByDay(1);// 获取当天结束时间
    const login_time = {$gte:time_start,$lt:time_end};
    result.push([
        "今日登陆数",await dbUtil.selectCount(context,"c_user_login",{login_time}),
    ]);
    result.push([
        "用户总数",await dbUtil.selectCount(context,"c_user",{}),
    ]);
    result.push([
        "会员总数",await dbUtil.selectCount(context,"c_user",{is_user_member: 1}),
    ]);
    result.push([
        "关注店铺人数",await dbUtil.selectCount(context,"c_user_shop_attention",{}),
    ]);
    result.push([
        "签到总次数",await dbUtil.selectCount(context,"c_user_sign",{}),
    ]);
    result.push([
        "收藏商品次数",await dbUtil.selectCount(context,"c_collect_goods",{}),
    ]);
    result.push([
        "浏览商品次数",await dbUtil.selectCount(context,"c_browse_goods",{}),
    ]);
    result.push([
        "分享次数",await dbUtil.selectCount(context,"c_share_app",{}),
    ]);
   
    result.push([
        "赠出积分(28号6点开始记录)",(await dbUtil.aggregate(context,"game_currency_log",
        {"$group":{_id: '',总共:{"$sum":"$gameCurrency"}}}
                        ))[0].总共
    ]);

    result.push([
        "用户当前用有积分",(await dbUtil.aggregate(context,"c_user",
        {"$group":{_id: '',总共:{"$sum":"$game_current_currency"}}}
                        ))[0].总共
    ]);
    
    
    return result;
}


/**
 * 获取统计数据
 */
exports.getExangeData = async (context) => {
    const result = [];
    
    result.push([
        "10元优惠券",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:1,award_source:3}),
    ]);
    result.push([
        "20元优惠券",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:2,award_source:3}),
    ]);
    result.push([
        "30元优惠券",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:3,award_source:3}),
    ]);
    result.push([
        "999元行李箱",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:4,award_source:3}),
    ]);
    result.push([
        "399元钱包礼盒",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:5,award_source:3}),
    ]);
    result.push([
        "299元腰包",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:6,award_source:3}),
    ]);
    result.push([
        "199元墨镜",await dbUtil.selectCount(context,"c_award_issue",
                                {award_type:7,award_source:3}),
    ]);
    
    return result;
}



/**
 * 导出抽奖数据
 */
exports.getExportData = async (context) => {
    let list = await dbUtil.selectListAll(context,"c_award_issue",
                                {award_type:{$in:[4,5,6,7]},award_source:3});
    return list;
}


/**
 * 获取抽奖统计数据
 */
exports.getAwardReversalData = async (context) => {
    const result = [];
    const source = 2;
    result.push([
        "一等奖 1110元超级福利券",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:7,source}),
    ]);
    result.push([
        "二等奖 50元优惠券",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:6,source}),
    ]);
    result.push([
        "三等奖 111个酷GUY币",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:5,source}),
    ]);
    result.push([
        "四等奖 33个酷GUY币",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:4,source}),
    ]);
    result.push([
        "五等奖 11个酷GUY币",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:3,source}),
    ]);
    result.push([
        "六等级 1个酷GUY币",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {type:2,source}),
    ]);

    result.push([
        "未中奖",await dbUtil.selectCount(context,"c_award_reversal_log",
                                {result:"未中奖"}),
    ]);
    return result;
}


/**
 * 清除商品id中的空格
 */
exports.clearSpaceForC_collect_goods_info = async (context) => {
    const tableName = "c_collect_goods_info";
    const list = await dbUtil.selectListAll(context,tableName,{},{});
    for(let item of list){
        item.goods_id = item.goods_id.trim();
        await dbUtil.updateById(context,tableName,item,item);
    }
    return list;
}

/**
 * 帮所有用户的剩余积分转移到真实积分
 */
exports.convertIntegralForUser = async (context) => {
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    
    let filter = {is_user_member:1,changeResult:{$exists:false}};
    let list = await dbUtil.selectListAll(context,"c_user",filter,{});
    
    let successNum =0;
    for(let item of list){
        if(!item.game_current_currency_is_done && item.game_current_currency>0){
            let changeResult = null;
            try {
                changeResult = await apiUtil.changePoint(context,merchant.access_token,item.open_id,"0",item.game_current_currency);
                //转换的真实积分
                item.game_current_currency_to_real = item.game_current_currency;
                item.game_current_currency_is_done = true;
                item.changeResult = changeResult;
                successNum++;
            } catch (error) {
                item.game_current_currency_to_real = 0;
                item.game_current_currency_is_done = true;

                item.changeResult = {msg:"非会员，不转换"};

                console.info("非用户不转换",item);
            }

            await dbUtil.updateById(context,"c_user",item,item);
        }
    }

    return {code:0,msg:"转换成功,本次转换"+successNum,first:list[0],successNum};
}

exports.isMember = async (context) => {
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    let isMember = await apiUtil.taobaoApiToMember(context,merchant.access_token,"我01jMicZrpKYUnzNQ9KyRUAXtc/NXfkqMXXW1tqcMJmu+E=");
    return isMember;
}